#include <QKeyEvent>

#include "recordform.h"
#include "ui_recordform.h"

#include "appmanager.h"

RecordForm::RecordForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecordForm)
{
    ui->setupUi(this);
}

RecordForm::~RecordForm()
{
    delete ui;
}

void RecordForm::prepareUi(const int id)
{
    ui->edtComment->clear();
    ui->edtName->clear();
    ui->edtUniqId->clear();

    // load all styles
    if (appManager->stylesLoaded() == AllStylesLoaded)
    {
        ui->cbStyle->clear();
        foreach(Style st, appManager->mStyles)
        {
            ui->cbStyle->addItem(QIcon(st.img), QString("%1. %2").arg(st.uniqId).arg(st.name));
        }
    }

    ui->edtUniqId->setText(QString("%1").arg(id));
}

void RecordForm::keyPressEvent(QKeyEvent *e)
{
  if(e->key()==Qt::Key_Escape)
    this->close();
}

void RecordForm::morph2AddRecordForm(const int id)
{
    qDebug()<<"IN RecordForm::morph2AddRecordForm";

    setWindowTitle("Вставити запис");
    prepareUi(id);

    if (!ui->btnAccept->disconnect(SIGNAL(clicked())))
    {
        qDebug()<<"Cannt disconect!";
//        return;
    }

    QObject::connect(this->ui->btnAccept, SIGNAL(clicked()),
                     this, SLOT(addRecord()));
}

void RecordForm::morph2ChangeRecordForm(const int id, QString name, const int styleId, QString comment)
{
    qDebug()<<"IN RecordForm::morph2ChangeRecordForm";

    setWindowTitle("Редагувати запис");
    prepareUi(id);

    ui->edtName->setText(name);
    ui->edtComment->setText(comment);
    ui->cbStyle->setCurrentIndex(styleId - 1);

    ui->btnAccept->disconnect(SIGNAL(clicked()));

    if (!ui->btnAccept->disconnect(SIGNAL(clicked())))
    {
        qDebug()<<"Cannt disconect!";
//        return;
    }

    QObject::connect(this->ui->btnAccept, SIGNAL(clicked()),
                     this, SLOT(changeRecord()));
}

void RecordForm::addRecord()
{
    qDebug()<<"IN AddRecordForm::on_btnAccept_clicked()";
    QString styleId(ui->cbStyle->currentText());
    styleId = styleId.left(styleId.indexOf("."));

    appManager->dbWorker()->insertRecord(
                ui->edtUniqId->text().toInt(),
                ui->edtName->text(),
                styleId.toInt(),
                ui->edtComment->text());

    prepareUi(appManager->dbWorker()->findNextFreeId());

    appManager->dbWorker()->refreshAllModels();
    appManager->mainWindow()->refreshViews();
}

void RecordForm::changeRecord()
{
    qDebug()<<"IN RecordForm::changeRecord()";
    QString styleId(ui->cbStyle->currentText());
    styleId = styleId.left(styleId.indexOf("."));

    bool res = false;
    res = appManager->dbWorker()->editCurRecord(
                ui->edtUniqId->text().toInt(),
                ui->edtName->text(),
                styleId.toInt(),
                ui->edtComment->text());

    if (res)
    {
        appManager->dbWorker()->refreshAllModels();
        appManager->mainWindow()->refreshViews();
        LOG ("RecordForm::changeRecord()", "Зміни успішно внесені", "" );
    }
    else
    {
        LOG ("RecordForm::changeRecord()", "Проблема при внесенні змін!", "" );
    }
}

void RecordForm::on_btnCancel_clicked()
{
    this->close();
}
